<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\mvc\admin;

use suifeng\builder\ListBuilder;
use suifeng\builder\FormBuilder;
use suifeng\helper\ClassHelper;
use suifeng\builder\PageWrapper;
use suifeng\builder\BoxWidget;

abstract class ModelController2 extends AdminController
{
    /**
     * 模型名称，必需项。
     */
    protected $model_name;

    /**
     * 模型字段，必需项。
     */
    protected $model_fields;

    /**
     * 数据类型与控件类型的对应关系。
     *
     * @var array
     */
    protected $data_type_map = array(
        'integer'       => 'number',
    );

    /**
     * 不可以修改的字段名称列表。
     *
     * @var array
     */
    protected $uneditable_field_names = array(
        'id', 'create_time', 'update_time',
    );

    /**
     * 缓存的模型字段。
     *
     * @var unknown
     */
    protected $cache_model_fields = null;

    /**
     * 初始化控制器。
     */
    protected function _initialize()
    {
        parent::_initialize();

        // 通用属性
        $this->model_object = model($this->model_name);
        $this->model_title = $this->_model_title();
        $this->model_fields = $this->_model_fields();
        $this->key_field = $this->model_object->getPk();

        // 列表属性
        $this->list_fields = $this->_get_model_property('list_fields');
        $this->list_actions = $this->_get_model_property('list_actions');
        $this->list_buttons = $this->_get_model_property('list_buttons');
        $this->default_page_size = $this->_get_model_property('default_page_size');
        $this->default_sort = $this->_get_model_property('default_sort');
        $this->default_sort_field = $this->_get_model_property('default_sort_field');
        $this->default_sort_order = $this->_get_model_property('default_sort_order');
        $this->avail_sort_fields = $this->_get_model_property('avail_sort_fields');
        $this->avail_search_fields = $this->_get_model_property('avail_search_fields');
        $this->avail_filter_fields = $this->_get_model_property('avail_filter_fields');
        $this->enable_search = $this->_get_model_property('enable_search');
        $this->enable_sort = $this->_get_model_property('enable_sort');
        $this->enable_filter = $this->_get_model_property('enable_filter');
        $this->enable_page = $this->_get_model_property('enable_page');
        $this->enable_select = $this->_get_model_property('enable_select');
        $this->extra_wheres = $this->_get_model_property('extra_wheres');

        // 表单属性
        $this->form_fields = $this->_model_form_fields();
        $this->form_layout = $this->_model_form_layout();
    }

    /**
     * 获取属性值，先检查控制器，再检查模型对象，最后返回默认值。
     *
     * @param unknown $property
     * @param unknown $default
     * @return unknown|\suifeng\utils\NULL|mixed|string
     */
    protected function _get_model_property($property, $default = null)
    {
        if (isset($this->$property) && $this->$property !== null) {
            return $this->$property;
        }

        $method_name = '_model_' . $property;
        if (method_exists($this, $method_name)) {
            if (($result = $this->$method_name()) !== null) {
                return $result;
            }
        }

        return $default;
    }

    /**
     * 返回模型对象标题。
     *
     * @return unknown|string
     */
    protected function _model_title()
    {
        if (isset($this->model_title) && $this->model_title) {
            return $this->model_title;
        } else {
            return \suifeng\helper\StringHelper::friendly_text($this->model_name);
        }
    }

    /**
     * 返回模型字段列表。
     *
     * @return array
     */
    protected function _model_fields()
    {
        // 本方法会初始化模型对象所有的字段属性。
        // 这是一个复杂的过程，所以会通过实例变量做一下缓存。

        if ($this->cache_model_fields !== null) {
            return $this->cache_model_fields;
        }

        $this->cache_model_fields = array();
        $model_fields = isset($this->model_fields) ? $this->model_fields : array();

        foreach ($model_fields as $field_name => $field_info) {
            $field_info['field'] = $field_name;

            if (empty($field_info['dbtype'])) {
                $field_info['dbtype'] = 'string';
            }

            if (empty($field_info['title'])) {
                if (isset($field_info['label'])) {
                    $field_info['title'] = $field_info['label'];
                } else {
                    $field_info['title'] = \suifeng\helper\StringHelper::friendly_text($field_name);
                }
            }

            if (empty($field_info['label'])) {
                if (isset($field_info['title'])) {
                    $field_info['label'] = $field_info['title'];
                } else {
                    $field_info['label'] = \suifeng\helper\StringHelper::friendly_text($field_name);
                }
            }

            if (empty($field_info['control'])) {
                if (isset($field_info['display'])) {
                    $field_info['control'] = $field_info['display'];
                } else if (isset($field_info['editable']) && $field_info['editable'] === true) {
                    $field_info['control'] = 'text';
                }
            }

            if (empty($field_info['column'])) {
                if (isset($field_info['display'])) {
                    $field_info['column'] = $field_info['display'];
                } else if (isset($field_info['listable']) && $field_info['listable'] === true) {
                    $field_info['column'] = 'text';
                }
            }

            $field_info['rules'] = $this->_init_field_rules($field_name, $field_info);
            $this->cache_model_fields[$field_name] = $field_info;
        }

        return $this->cache_model_fields;
    }

    /**
     * 初始化字段验证规则。
     *
     * @param unknown $field_name
     * @param unknown $field_info
     * @return array
     */
    private function _init_field_rules($field_name, $field_info)
    {
        $field_rules = $default_rules = array();

        foreach (isset($field_info['rules']) ? $field_info['rules'] : [] as $type => $config) {
            if (is_string($config)) {
                if (is_int($type)) {
                    $config = ['rule' => $config];
                } else {
                    $config = ['rule' => $type, 'message' => $config];
                }
            } else {
                if (is_string($type)) {
                    $config['rule'] = $type;
                }
            }
            $field_rules[] = $config;
        }

        if (isset($field_info['required']) && $field_info['required'] === true && !$this->_check_rule_exists($field_rules, 'required')) {
            $default_rules[] = ['rule' => 'required'];
        }

        if ($field_info['dbtype'] == 'string' && isset($field_info['length']) && $field_info['length'] > 0 && !$this->_check_rule_exists($field_rules, 'length')) {
            $default_rules[] = ['rule' => 'length', 'max' => intval($field_info['length'])];
        }

        if (isset($field_info['unique']) && $field_info['unique'] === true && !$this->_check_rule_exists($field_rules, 'unique')) {
            $default_rules[] = ['rule' => 'unique'];
        }

        return array_merge($default_rules, $field_rules);
    }

    /**
     * 检查指定的rule是否存在。
     *
     * @param unknown $rule_list
     * @param unknown $check_rule
     * @return boolean
     */
    private function _check_rule_exists($rule_list, $check_rule)
    {
        foreach ($rule_list as $rule_config) {
            if (isset($rule_config['rule']) && $rule_config['rule'] == $check_rule) {
                return true;
            }
        }
        return false;
    }

    /**
     * 返回可搜索字段列表。
     *
     * @return array
     */
    protected function _model_avail_search_fields()
    {
        if (isset($this->avail_search_fields) && $this->avail_search_fields) {
            return $this->avail_search_fields;
        }

        // 查询searchable字段
        $search_fields = array();

        foreach ($this->_model_fields() as $name => $field) {
            if (isset($field['searchable']) && $field['searchable'] === true) {
                $search_fields[] = $name;
            }
        }

        return $search_fields;
    }

    /**
     * 返回可过滤字段列表。
     *
     * @return array
     */
    protected function _model_avail_filter_fields()
    {
        if (isset($this->avail_filter_fields) && $this->avail_filter_fields) {
            return $this->avail_filter_fields;
        }

        // 查询sortable字段
        $filter_fields = array();

        foreach ($this->_model_fields() as $name => $field) {
            if (isset($field['filterable']) && $field['filterable'] === true) {
                $filter_fields[] = $name;
            }
        }

        return $filter_fields;
    }

    /**
     * 返回可排序字段列表。
     *
     * @return array
     */
    protected function _model_avail_sort_fields()
    {
        if (isset($this->avail_sort_fields) && $this->avail_sort_fields) {
            return $this->avail_sort_fields;
        }

        // 查询sortable字段
        $sort_fields = array();

        foreach ($this->_model_fields() as $name => $field) {
            if (isset($field['sortable']) && $field['sortable'] === true) {
                $sort_fields[] = $name;
            }
        }

        return $sort_fields;
    }

    /**
     * 返回是否允许搜索？
     *
     * @return boolean
     */
    protected function _model_enable_search()
    {
        if (isset($this->enable_search)) {
            return $this->enable_search;
        } else {
            $search_fields = $this->_model_avail_search_fields();
            return !empty($search_fields);
        }
    }

    /**
     * 返回是否允许排序？
     *
     * @return boolean
     */
    protected function _model_enable_sort()
    {
        if (isset($this->enable_sort)) {
            return $this->enable_sort;
        } else {
            $sort_fields = $this->_model_avail_sort_fields();
            return !empty($sort_fields);
        }
    }

    /**
     * 返回是否允许过滤？
     *
     * @return boolean
     */
    protected function _model_enable_filter()
    {
        if (isset($this->enable_filter)) {
            return $this->enable_filter;
        } else {
            $filter_fields = $this->_model_avail_filter_fields();
            return !empty($filter_fields);
        }
    }

    /**
     * 返回是否允许分页？
     *
     * @return boolean
     */
    protected function _model_enable_page()
    {
        if (isset($this->enable_page)) {
            return $this->enable_page;
        } else {
            return true;
        }
    }

    /**
     * 返回是否允许选择？
     *
     * @return boolean
     */
    protected function _model_enable_select()
    {
        if (isset($this->enable_select)) {
            return $this->enable_select;
        } else {
            return true;
        }
    }

    /**
     * 返回默认排序。
     *
     * @return null|string
     */
    protected function _model_default_sort()
    {
        if (isset($this->default_sort) && $this->default_sort) {
            return $this->default_sort;
        } else {
            return null;
        }
    }

    /**
     * 返回默认排序字段。
     *
     * @return null|string
     */
    protected function _model_default_sort_field()
    {
        $default_sort = $this->_model_default_sort();
        if (empty($default_sort)) {
            return null;
        }

        // 多个排序字段，处理不了
        if (strpos($default_sort, ',') !== false) {
            return null;
        }

        list($field, $order) = exp(' ', $default_sort);
        return trim($field);
    }

    /**
     * 返回默认排序顺序。
     *
     * @return null|string
     */
    protected function _model_default_sort_order()
    {
        $default_sort = $this->_model_default_sort();
        if (empty($default_sort)) {
            return null;
        }

        // 多个排序字段，处理不了
        if (strpos($default_sort, ',') !== false) {
            return null;
        }

        list($field, $order) = exp(' ', $default_sort, 2);
        return empty($order) ? 'asc' : trim($order);
    }

    /**
     * 返回默认页面大小。
     *
     * @return null|string
     */
    protected function _model_default_page_size()
    {
        if (isset($this->default_page_size) && $this->default_page_size) {
            return $this->default_page_size;
        } else {
            return config('ADMIN_PAGE_ROWS');
        }
    }

    /**
     * 返回默认数据。
     *
     * @return null|string
     */
    protected function _model_default_data_array()
    {
        $default_data = array();
        foreach ($this->_model_fields() as $name => $field) {
            if (isset($field['default'])) {
                $default_data[$name] = $field['default'];
            } else {
                if (isset($field['identity']) && $field['identity'] === true) {
                    $default_data[$name] = null;
                } else {
                    $default_data[$name] = \suifeng\helper\ClassHelper::default_value($field['dbtype']);
                }
            }
        }
        return $default_data;
    }

    /**
     * 返回列表字段列表。
     *
     * @return array
     */
    protected function _model_list_fields()
    {
        if (isset($this->list_fields) && $this->list_fields) {
            return $this->list_fields;
        }

        // 查询listable字段
        $list_fields = array();

        foreach ($this->_model_fields() as $name => $field) {
            if (isset($field['listable']) && $field['listable'] === true) {
                $list_fields[] = $name;
            }
        }

        return $list_fields;
    }

    /**
     * 返回表单字段列表。
     *
     * @return array
     */
    protected function _model_form_fields()
    {
        if (isset($this->form_fields) && $this->form_fields) {
            return $this->form_fields;
        }

        // 查询editable字段
        $form_fields = array();

        foreach ($this->_model_fields() as $name => $field) {
            if (isset($field['editable']) && $field['editable'] === true) {
                $form_fields[] = $name;
            }
        }

        return $form_fields;
    }

    /**
     * 返回表单字段布局。
     *
     * @return array
     */
    protected function _model_form_layout()
    {
        if (isset($this->form_layout) && $this->form_layout) {
            return $this->form_layout;
        }

        // 没有设置form_layout，就直接返回null，让表单对象自动创建。
        return null;
    }

    /**
     * 返回列表行操作列表。
     *
     * @return null|string
     */
    protected function _model_list_actions()
    {
        if (isset($this->list_actions) && $this->list_actions) {
            return $this->list_actions;
        } else {
            return ['modify', 'delete'];
        }
    }

    /**
     * 返回列表按钮列表。
     *
     * @return null|string
     */
    protected function _model_list_buttons()
    {
        if (isset($this->list_buttons) && $this->list_buttons) {
            return $this->list_buttons;
        } else {
            return ['create'];
        }
    }

    /**
     * 返回URL地址。
     *
     * @param unknown $url
     * @param array $args
     * @return string
     */
    protected function _url($url, $args = [])
    {
        $params = array();

        foreach ($this->request->param() as $key => $value) {
            if (substr($key, 0, 2) == 'f_' || substr($key, 0, 2) == 's_') {
                $params[$key] = $value;
            }
        }

        $params = array_merge($params, $args);
        return url($url, $params);
    }

    /**
     * 返回字段相关信息。
     *
     * @param unknown $name
     * @return array
     */
    protected function _get_field_info($fied_name)
    {
        $model_fields = $this->_model_fields();
        if (isset($model_fields[$fied_name])) {
            return $model_fields[$fied_name];
        } else {
            return array();
        }
    }

    public function index()
    {
        $list_builder = $this->_create_list_builder();
        $this->_handle_list_init($list_builder);
        $this->_handle_list_data($list_builder);

        $html = $this->_handle_list_html($list_builder);

        $tpl_name = 'admin@public/model_list';
        $tpl_vars['content'] = $html;
        $tpl_vars['title'] = $this->model_title . '列表';
        $this->display($tpl_name, $tpl_vars);

//         $box_widget = new BoxWidget();
//         $box_widget->content[] = $list_builder;
//         echo new PageWrapper($this, $box_widget);
    }

    protected function _create_list_builder()
    {
        $list_builder = new ListBuilder();
        $list_builder->controller = $this;
        $list_builder->model_object = $this->model_object;
        $list_builder->key_field = $this->key_field;
        $list_builder->avail_search_fields = $this->avail_search_fields;
        $list_builder->avail_sort_fields = $this->avail_sort_fields;
        $list_builder->avail_filter_fields = $this->avail_filter_fields;
        $list_builder->enable_search = $this->enable_search;
        $list_builder->enable_sort = $this->enable_sort;
        $list_builder->enable_filter = $this->enable_filter;
        $list_builder->enable_page = $this->enable_page;;
        $list_builder->default_sort_field = $this->default_sort_field;
        $list_builder->default_sort_order = $this->default_sort_order;
        $list_builder->default_page_size = $this->default_page_size;
        $list_builder->page_url = $this->_url('index', ['s_page' => '[page]']);
        $list_builder->extra_wheres = $this->extra_wheres;

        if ($this->enable_select) {
            $list_builder->add_column('_selected_', 'selected');
        }

        foreach ($this->list_fields as $field => $config) {
            if (is_string($config)) {
                $field = $config;
                $config = array();
            }

            $config['field'] = $field;
            $field_info = $this->_get_field_info($field);
            $config = array_merge($field_info, (array) $config);
            $list_builder->add_column($field, $config['column'], null, (array)$config);
        }

        if ($this->list_actions) {
            $list_builder->add_column('_actions_', 'actions');

            foreach ($this->list_actions as $name => $config) {
                if (is_string($config)) {
                    $name = $config;
                    $config = array();
                }

                if (isset($config['type'])) {
                    $type = $config['type'];
                    unset($config['type']);
                } else {
                    $type = null;
                }

                $list_builder->add_action($name, $type, null, (array)$config);
            }
        }

        foreach ($this->list_buttons as $type => $config) {
            if (is_string($config)) {
                $name = $config;
                $config = array();
            }

            if (isset($config['type'])) {
                $type = $config['type'];
                unset($config['type']);
            } else {
                $type = null;
            }

            $list_builder->add_button($name, $type, null, (array)$config);
        }

        $list_builder->data_list = null;
        $list_builder->rows_count = 0;
        return $list_builder;
    }

    protected function _handle_list_init($list_builder)
    {
        $list_builder->data_list = null;
        $list_builder->rows_count = 0;
    }

    protected function _handle_list_data($list_builder)
    {
        $count_query = $this->model_object->db();
        $this->_set_list_query_where($list_builder, $count_query);
        $list_builder->rows_count = $count_query->count();

        $list_query = $this->model_object->db();
        $this->_set_list_query_where($list_builder, $list_query);
        $this->_set_list_query_page_and_order($list_builder, $list_query);
        $list_builder->data_list = $list_query->select();
    }

    protected function _handle_list_html($list_builder)
    {
        return $list_builder->render_html();
    }

    protected function _set_list_query_where($list_builder, $query)
    {
        $query->where($list_builder->get_where_map());
    }

    protected function _set_list_query_page_and_order($list_builder, $query)
    {
        if ($list_builder->enable_page) {
            $query->page($list_builder->page_number, $list_builder->page_size);
        }

        if ($list_builder->enable_sort && $list_builder->sort_field && $list_builder->sort_order) {
            $query->order($list_builder->sort_field, $list_builder->sort_order);
        } else if ($this->default_sort) {
            $query->order($this->default_sort);
        }
    }

    /**
     * 创建
     */
    public function create()
    {
        $form_builder = $this->_create_form_builder();
        $this->_handle_form_init($form_builder);
        $this->_handle_form_data($form_builder);

        if (request()->isPost()) {
            $this->_handle_form_validate($form_builder);
            if ($form_builder->is_valid()) {
                $this->_handle_form_save($form_builder);
                $this->success('新增成功', url('index'));
            }
        }

        $html = $this->_handle_form_html($form_builder);

        $tpl_name = 'admin@public/model_form';
        $tpl_vars['content'] = $html;
        $tpl_vars['meta_title'] = '新增' . $this->model_title;
        $tpl_vars['title'] = '新增' . $this->model_title;
        $this->display($tpl_name, $tpl_vars);




//         $this->model_form = $this->_create_form_builder();
//         $this->_handle_form_init();

//         $this->model_form->original_data = $this->_model_default_data_array();
//         $this->_handle_form_original();

//         if (request()->isPost()) {
//             $merged_data = array_merge($this->model_form->original_data, $_POST);
//             $this->model_form->init_submitted_data($merged_data);
//         } else {
//             $this->model_form->init_submitted_data($this->model_form->original_data);
//         }

//         $this->_handle_form_ready();

//         if (request()->isPost()) {
//             $this->model_form->validate();
//             $this->_handle_form_validate();

//             if ($this->model_form->is_valid) {
//                 $this->_handle_form_before_insert();
//                 $this->_handle_form_before_save();
//                 $submitted_data = $this->model_form->submitted_data;

//                 if ($this->model_object->insert($submitted_data)) {
//                     $this->_handle_form_after_insert();
//                     $this->_handle_form_after_save();
//                     $this->success('新增成功', url('index'));
//                 } else {
//                     $this->error($this->model_objec->getError());
//                 }
//             }
//         }

//         $this->content = $this->model_form->render_html();
//         $this->_handle_form_content();

//         $tpl_name = 'admin@public/model_form';
//         $tpl_vars['content'] = $this->content;
//         $tpl_vars['meta_title'] = '新增' . $this->model_title;
//         $tpl_vars['title'] = '新增' . $this->model_title;
//         $this->display($tpl_name, $tpl_vars);
    }

    /**
     * 修改
     */
    public function modify()
    {
        $form_builder = $this->_create_form_builder();
        $this->_handle_form_init($form_builder);
        $this->_handle_form_data($form_builder);

        if (request()->isPost()) {
            $this->_handle_form_validate($form_builder);
            if ($form_builder->is_valid()) {
                $this->_handle_form_save($form_builder);
                $this->success('修改成功', url('index'));
            }
        }

        $html = $this->_handle_form_html($form_builder);

        $tpl_name = 'admin@public/model_form';
        $tpl_vars['content'] = $html;
        $tpl_vars['meta_title'] = '修改' . $this->model_title;
        $tpl_vars['title'] = '修改' . $this->model_title;
        $this->display($tpl_name, $tpl_vars);




//         $this->model_form = $this->_create_form_builder();
//         $this->_handle_form_init();

//         $where_map[$this->model_form->key_field] = $this->model_form->key_value;
//         $this->model_form->original_data = $this->model_object->where($where_map)->findOrFail()->getData();
//         $this->_handle_form_original();

//         if (request()->isPost()) {
//             $merged_data = array_merge($this->model_form->original_data, $_POST);
//             $this->model_form->init_submitted_data($merged_data);
//         } else {
//             $this->model_form->init_submitted_data($this->model_form->original_data);
//         }

//         $this->_handle_form_ready();

//         if (request()->isPost()) {
//             $this->model_form->validate();
//             $this->_handle_form_validate();

//             if ($this->model_form->is_valid) {
//                 $this->_handle_form_before_update();
//                 $this->_handle_form_before_save();
//                 $submitted_data = $this->model_form->submitted_data;

//                 if ($this->model_object->update($submitted_data, $where_map)) {
//                     $this->_handle_form_after_update();
//                     $this->_handle_form_after_save();
//                     $this->success('修改成功', url('index'));
//                 } else {
//                     $this->error($this->model_objec->getError());
//                 }
//             }
//         }

//         $this->content = $this->model_form->render_html();
//         $this->_handle_form_content();

//         $tpl_name = 'admin@public/model_form';
//         $tpl_vars['content'] = $this->content;
//         $tpl_vars['meta_title'] = '修改' . $this->model_title;
//         $tpl_vars['title'] = '修改' . $this->model_title;
//         $this->display($tpl_name, $tpl_vars);
    }

    /**
     * 创建表单对象。
     *
     * @return \suifeng\html\FormBuilder
     */
    protected function _create_form_builder()
    {
        $form_builder = new FormBuilder();
        $form_builder->model_object = $this->model_object;
        $form_builder->key_field = $this->key_field;
        $form_builder->layout = $this->form_layout;

        foreach ($this->form_fields as $field => $config) {
            if (is_string($config)) {
                $field = $config;
                $config = array();
            }

            $config['field'] = $field;
            $field_info = $this->_get_field_info($field);
            $config = array_merge($field_info, (array) $config);
            $form_builder->add_control($field, $config['control'], null, $config);
        }

        return $form_builder;
    }

    protected function _handle_form_init($form_builder)
    {
        $form_builder->key_value = input($form_builder->key_field);
    }

    protected function _handle_form_data($form_builder)
    {
        if (empty($form_builder->key_value)) {
            $form_builder->initialize($this->_model_default_data_array());
        } else {
            $where_map[$form_builder->key_field] = $form_builder->key_value;
            $form_builder->initialize($this->model_object->where($where_map)->findOrFail()->getData());
        }
    }

    protected function _handle_form_validate($form_builder)
    {
        $form_builder->validate();
    }

    protected function _handle_form_save($form_builder)
    {
        $acceptted_data = $form_builder->acceptted_data;

        if (empty($form_builder->key_value)) {
            if (!$this->model_object->insert($acceptted_data)) {
                $this->error($this->model_objec->getError());
            }
        } else {
            $where_map[$form_builder->key_field] = $form_builder->key_value;
            if (!$this->model_object->update($acceptted_data, $where_map)) {
                $this->error($this->model_objec->getError());
            }
        }
    }

    protected function _handle_form_html($form_builder)
    {
        return $form_builder->render_html();
    }







//     protected function _handle_form_original() {}
//     protected function _handle_form_ready() {}
//     protected function _handle_form_content() {}


//     protected function _handle_form_before_save() {}
//     protected function _handle_form_after_save() {}
//     protected function _handle_form_before_update() {}
//     protected function _handle_form_after_update() {}
//     protected function _handle_form_before_insert() {}
//     protected function _handle_form_after_insert() {}
//     protected function _handle_form_before_delete() {}
//     protected function _handle_form_after_delete() {}
//     protected function _handle_model_list() {}

    /**
     * 启用。
     */
    public function enable()
    {
        $model_object = $this->_get_model_object();

        $key_field = $this->_get_key_field();
        $id_list = input($key_field . '/a');

        $where_map[$key_field] = array('in', $id_list);
        $this->model_list = $model_object->where($where_map)->selectOrFail();
        $this->_handle_model_list();

        $model_object->where($where_map)->update(['status' => STATUS_ENABLED]);
        $this->success('启用成功', url('index'));
    }

    /**
     * 禁用。
     */
    public function disable()
    {
        $model_object = $this->_get_model_object();

        $key_field = $this->_get_key_field();
        $id_list = input($key_field . '/a');

        $where_map[$key_field] = array('in', $id_list);
        $this->model_list = $model_object->where($where_map)->selectOrFail();
        $this->_handle_model_list();

        $model_object->where($where_map)->update(['status' => STATUS_DISABLED]);
        $this->success('禁用成功', url('index'));
    }


    /**
     * 表格头，可以增加一些额外内容。
     *
     * @return NULL
     */
    protected function _fetch_view_list_header()
    {
        return null;
    }

    /**
     * 表格尾，可以增加一些额外内容。
     *
     * @return NULL
     */
    protected function _fetch_view_list_footer()
    {
        return null;
    }
}
